package com.panopset;

import static com.panopset.Util.*;

import java.awt.Color;

import javax.swing.JTextField;

import com.panopset.gui.LookAndFeel;

/**
 * User alert mechanism.
 *
 * @author Karl Dinwiddie
 *
 */
public class Alert {
    public Color alertColor = LookAndFeel.DSPMSG_GREEN_ALERT;
    private String redMsg = EMPTY_STRING;
    private String yellowMsg = EMPTY_STRING;
    private String greenMsg = EMPTY_STRING;

    private void update() {
        if (isPopulated(redMsg)) {
            alertColor = LookAndFeel.DSPMSG_RED_ALERT;
            getMsgLabel().setText(redMsg);
        } else if (isPopulated(yellowMsg)) {
            alertColor = LookAndFeel.DSPMSG_YELLOW_ALERT;
            getMsgLabel().setText(yellowMsg);
        } else if (isPopulated(greenMsg)) {
            alertColor = LookAndFeel.DSPMSG_GREEN_ALERT;
            getMsgLabel().setText(greenMsg);
        } else {
            singleton.getMsgLabel().setText(EMPTY_STRING);
        }
    }

    public static void clearAll() {
        singleton.redMsg = EMPTY_STRING;
        singleton.yellowMsg = EMPTY_STRING;
        singleton.greenMsg = EMPTY_STRING;
    }

    public static void clearRed() {
        singleton.redMsg = EMPTY_STRING;
        singleton.update();
    }

    public static void clearYellow() {
        singleton.yellowMsg = EMPTY_STRING;
        singleton.update();
    }

    public Color getAlertColor() {
        return alertColor;
    }

    public static void red(String msg) {
        singleton.redMsg = msg;
        singleton.update();
    }

    public static void yellow(String msg) {
        singleton.yellowMsg = msg;
        singleton.update();
    }

    public static void green(String msg) {
        singleton.greenMsg = msg;
        singleton.update();
    }

    public static void clearTopMessage() {
        singleton.clearTopMessage00();
    }

    private void clearTopMessage00() {
        if (isPopulated(redMsg)) {
            red(EMPTY_STRING);
        } else if (isPopulated(yellowMsg)) {
            yellow(EMPTY_STRING);
        } else if (isPopulated(greenMsg)) {
            green(EMPTY_STRING);
        }
    }

    public static String getMessage() {
        singleton.update();
        return singleton.getMsgLabel().getText();
    }

    public static Color getColor() {
        singleton.update();
        return singleton.getAlertColor();
    }

    private JTextField msgLabel;
    public JTextField getMsgLabel() {
        if (msgLabel == null) {
            msgLabel = new JTextField();
            msgLabel.setFont(Util.ARIAL_PLAIN_10);
            msgLabel.setEditable(false);
        }
        return msgLabel;
    }
    public static final Alert singleton = new Alert();
}
